import bot from "../../model/bot/amesu";
import { Bot_msg } from "../../model/bot/bot_msg";
import server from "../../model/client/server";
import { Emitter_bot, mathType, packge } from "../../model/msgHandle/packge";

// export let loginState: Map<string, Bot_msg> = new Map();
/**
 * 所有消息入口
 * 分析用户意图
 * 服务器连接状态
 * 保存状态
 */
class index extends packge {

    constructor() {
        super()
    }
    init(): void {
        this.register_botLiset(Emitter_bot.user_input, (msg) => { this.userInpit(msg) });
        // this.register_botLiset(Emitter_bot.login_succress, () => { this.botRead() });
        // this.register_botLiset(Emitter_bot.server_off, () => { loginState = new Map() });
    }
    async userInpit(msg: Bot_msg) {
        console.debug('用户发来消息', msg.getName(), msg.getContent());
        if (!msg.getContent()) {
            return;
        }
        bot.addMsgId(msg)
        // 分析意图
        let list = this.get_mathList();
        let miss = true;
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            try {
                if (element.mathType == mathType.math_all && element.mathStr == msg.getContent()) {
                    msg.content = msg.content.replace(element.mathStr, '')
                    miss = false;
                    element.action.call(element.self, msg);
                    return;
                }
                if (element.mathType == mathType.math_start && msg.getContent().indexOf(element.mathStr) == 0) {
                    msg.content = msg.content.replace(element.mathStr, '')
                    miss = false;
                    element.action.call(element.self, msg);
                    return;
                }
            } catch (error) {
                console.log('出错了')
            }

        }
        if (miss) {
            // 未命中消息转发到服务器分析
            server.api('body/Tick', { msg: msg.getContent(true) }, msg)

        }
    }
}
export default new index();